Những câu hỏi liên quan
Phúc
Xem chi tiết
ẻmafe
Xem chi tiết
Nguyễn Công Bằng
Xem chi tiết
APOK FF
Xem chi tiết
TRIẾT PHẠM
Xem chi tiết
Tiếng anh123456
Xem chi tiết
Phía sau một cô gái
11 tháng 8 2023 lúc 21:32

#include <iostream>

#include <vector>

using namespace std;

pair<int, int> findMaxSubarray(vector<int> nums) {

     int n = nums.size();

     int maxSum = nums[0];

     int currentSum = nums[0];

     int start = 0;

     int end = 0;

     for (int i = 1; i < n; i++) {

          if (currentSum < 0) {

               currentSum = nums[i];

               start = i;

               end = i;

          } else {

               currentSum += nums[i];

               end = i;

          }

          if (currentSum > maxSum) {

               maxSum = currentSum;

          }

     }

     return make_pair(start, end);

}

int main() {

     int numTests;

     cin >> numTests;

     for (int t = 0; t < numTests; t++) {

          int n;

          cin >> n;

          vector<int> nums(n);

          for (int i = 0; i < n; i++) {

               cin >> nums[i];

          }

          pair<int, int> maxSubarray = findMaxSubarray(nums);

          cout << maxSubarray.first << " " << maxSubarray.second << endl;

     }

     return 0;

}

Bình luận (0)
APOK FF
Xem chi tiết
kankan
Xem chi tiết
Ruby Võ
14 tháng 12 2016 lúc 21:00

đề sai thì phải

Bình luận (0)
Lala
Xem chi tiết